#coding: utf-8
class CulturalHeritage::Culture < ActiveRecord::Base
  validates :name, :presence => {:message => "El nombre es requerido"}
  validates :period, :presence => {:message => "El Periodo es requerido"}
  default_scope where("deleted = 0")
  belongs_to :obj_period, :class_name => 'CulturalHeritage::Period',  :foreign_key => 'period'
  has_many :list_point, :class_name => 'CulturalHeritage::CulturePoint',  :foreign_key => 'culture'
  has_many :list_cultural_heritages, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'culture'

  def self.count_period_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_cultures.period = ? and (cultural_heritage_cultures.deleted = ?) ",id,0],:select=>"count(*) as 'cultures'")[0].cultures.to_i
  end

  def erasable
    cultu = CulturalHeritage::CulturalHeritage.count_culture_dependencies(id)
    point = CulturalHeritage::CulturePoint.count_culture_dependencies(id)
    if cultu >0
      return "Imposible borrar. Existen dependencias en patrimonio cultural"    
    end
    if point >0
      return "Imposible borrar. Existen dependencias en georeferencia de cultura"    
    end
    return nil
  end
end
